package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.PacApplicantEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.model.dto.WebInvalidAnalysisInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebPacApplicantInfoDto;
import com.baitengsoft.patentapimonitor.api.model.param.SearchApplicantParam;
import com.baitengsoft.patentapimonitor.api.service.*;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * Created by QEK on 2017/12/21.
 */
@RestController
@RequestMapping("/api/v1/web/applicantmanage")
@Api(value = "前台-系统管理-权利人管理",description = "权利人管理")
public class WebApplicantManageController extends AbstractController{
    @Autowired
    private RptApplicantService rptApplicantService;
    @Autowired
    private WebPlatformareaService webPlatformareaService;
    @Autowired
    private PacIndustryService pacIndustryService;
    @Autowired
    private CfgDictionaryService cfgDictionaryService;
    @Autowired
    private PacApplicantService pacApplicantService;
    @Autowired
    private WebExcelExportService webExcelExportService;


    /**
     * 获取筛选参数
     */
    @ApiOperation(value ="获取筛选参数（webAreaInfos=区域，WebIndustryInfo=行业,CfgDictionaryInfo=权利人类型,DataTypeInfo =数据范围 ）")
    @GetMapping("/ApplicantSearchParam")
    @ResponseBody
    public Res getApplicantSearchParam(){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        //获取行业name和行业code
        List<WebIndustryInfo> WebIndustryInfo = pacIndustryService.getIndustryInfoByPlatformId(userInfo.getPlatformid());
        //获取权力人类型和权力人code
        List<WebAreaInfo> webAreaInfos = webPlatformareaService.getNameAndIdByPlatformId(userInfo.getPlatformid());
        //获取区域name和区域ID
        List<CfgDictionaryInfo> CfgDictionaryInfo = cfgDictionaryService.getNameAndValueByCode("1004");
        //获取数据范围
        List<RptApplicantDataTypeInfo> DataTypeInfo = rptApplicantService.getApplicantDataType();
        return Res.ok().put("webAreaInfos",webAreaInfos).put("WebIndustryInfo",WebIndustryInfo).put("CfgDictionaryInfo",CfgDictionaryInfo).put("DataTypeInfo",DataTypeInfo);
    }


    @ApiOperation(value ="获取权利人列表")
    @GetMapping("/list")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantName", value = "权利人名字", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "address", value = "地址", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "industryId", value = "行业Id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "type", name = "type", value = "1,企业，2个人，3，院校，4科研机构，5机关团体，6其他", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)

    })
    public Res pageQuery(@ApiIgnore SearchApplicantParam param){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        PageResult<PacApplicantInfo> page = pacApplicantService.queryList(userInfo.getPlatformid(),param);
        return Res.ok().put("list",page);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "applicantId", value = "权力人ID", required = true)
    })
    @ApiOperation(value = "点击【操作】按钮，根据Id查询信息")
    @GetMapping(value = "/info/{applicantId}")
    public Res info (@PathVariable("applicantId") Integer applicantId){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if (applicantId == null){
            return Res.error("参数有误");
        }
        Integer platfromId= pacApplicantService.getPlatformIdByApplicantId(applicantId);
        if (platfromId == null || !userInfo.getPlatformid().equals(platfromId)){
            return Res.error("参数有误");
        }
        PacApplicantInfo info =  pacApplicantService.getApplicantInfo(applicantId);
        return Res.ok().put("info",info);
    }



    @ApiOperation(value = "维护")
    @PostMapping(value = "/update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "applicantId", value = "权力人ID", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "industryIds", value = "行业Id列表，逗号隔开", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = true)
    })
    public Res updateapplicant(@ApiIgnore PacApplicantInfo info){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if (info == null || info.getApplicantId()==null ||info.getAreaId()==null || info.getIndustryIds() == null) return Res.error("参数有误");
        PacApplicantEntity entity = pacApplicantService.queryById(info.getApplicantId());
        if (entity == null) return Res.error("未查询到该权力人");
        if (!entity.getPlatformid().equals(userInfo.getPlatformid()))  return Res.error("该权力人不属于本区域，修改失败");
        return pacApplicantService.updatePacApplicant(userInfo.getPlatformid(),info);
    }


    @ApiOperation(value = "区域归类")
    @PostMapping(value = "/classifyarea")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantIds", value = "权力人ID,逗号隔开", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = true)
    })
    public Res classifyArea(@ApiIgnore Integer[] applicantIds,@ApiIgnore Integer areaId){
        if (applicantIds == null) return  Res.error("参数有误");
        List<Integer> ids = Arrays.asList(applicantIds);
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if (ids.size()<1){
            return Res.error("请选择要操作的数据");
        }
        if (areaId == null || areaId<0){
            return Res.error("数据有误");
        }
        List<Integer> areaIds = webPlatformareaService.getAreaIdsByPlatformId(userInfo.getPlatformid());
        if (!areaIds.contains(areaId)){
            return Res.error("参数有误");
        }
        //查询该平台下所有权力人ID
        List<Integer> idlist = pacApplicantService.getApplicantIdsByPlatfromId(userInfo.getPlatformid());
        if (!idlist.containsAll(ids)){
            return Res.error("参数有误");
        }
        pacApplicantService.classifyAreaByIds(ids,areaId);
        return Res.ok();
    }

    @ApiOperation(value = "行业归类")
    @PostMapping(value = "/classifyindustry")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantIds", value = "权力人ID,逗号隔开", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "industryIds", value = "行业，逗号隔开", required = true)
    })
    public Res classifyIndustry(@ApiIgnore Integer[] applicantIds,@ApiIgnore Integer[] industryIds){
        List<Integer> applicantIdList = Arrays.asList(applicantIds);
        List<Integer> industryIdList = Arrays.asList(industryIds);
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if (applicantIdList.size()<1){
            return Res.error("请选择要操作的数据");
        }
        if (industryIdList.size()<1){
            return Res.error("数据有误");
        }
        //查询该平台下行业
        List<Integer> inIds = pacIndustryService.getIndustryIdsByPlatformId(userInfo.getPlatformid());
        if (!inIds.containsAll(industryIdList)){
            return Res.error("参数有误");
        }
        //查询该平台下所有权力人ID
        List<Integer> idlist = pacApplicantService.getApplicantIdsByPlatfromId(userInfo.getPlatformid());
        if (!idlist.containsAll(applicantIdList)){
            return Res.error("参数有误");
        }
        pacApplicantService.classifyIndustry(applicantIdList,industryIdList);
        return Res.ok();
    }


    @ApiOperation(value ="导出")
    @GetMapping("/listexport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantName", value = "权利人名字", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "address", value = "地址", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "industryIds", value = "行业Id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "type", name = "type", value = "1,企业，2个人，3，院校，4科研机构，5机关团体，6其他", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)

    })
    public Res listexport(@ApiIgnore SearchApplicantParam param, HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }

        PageResult<PacApplicantInfo> result = pacApplicantService.queryList(userInfo.getPlatformid(),param);
        if (result == null || result.getItems() == null || result.getItems().size() < 1) {
            return Res.error("无数据");
        }
        List<PacApplicantInfo> infos = result.getItems();
        List<WebPacApplicantInfoDto> webDtos = webExcelExportService.getWebPacApplicantInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "失效分析.xls",
                "失效分析");
    }
}
